package com.showgood.model {
	import com.showgood.config.Config;
	import com.showgood.config.PopupConfig;
	import com.showgood.config.SangoConst;
	import com.showgood.config.SangoEvent;
	import com.showgood.core.AbstractMediator;
	import com.showgood.core.AbstractProxy;
	import com.showgood.core.GameEvent;
	import com.showgood.core.NotificationEvent;
	import com.showgood.util.GameUtil;
	import com.showgood.util.PopupHolder;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	import org.puremvc.as3.interfaces.IMediator;
	import spark.components.Application;
	import spark.events.TitleWindowBoundsEvent;
	
	/**
	 * 弹窗做成一个栈
	 * 
	 * @author BianPeng
	 */
	public class PopupStackProxy extends AbstractProxy {
		private var _app:Application;	// base window
		private var _stack:Vector.<PopupHolder> = new Vector.<PopupHolder>();	// stack container
		public function PopupStackProxy(app:Application) {
			super();
			_app = app;
		}
		public function center():PopupStackProxy {
			PopUpManager.centerPopUp(topwindow.window);
			return this;
		}
		/**
		 * 顶层窗口
		 */
		public function get topwindow():PopupHolder {
			return _stack.length > 0 ? _stack[0] : null;
		}
		//
		// push window
		//
		public function pushWindow(id:int):PopupStackProxy {
			var _cfg:PopupConfig = PopupConfig.config(id);
			GameUtil.assert('popup config should not null', _cfg != null);
			while (_stack.length > 0) {
				if (topwindow.privilege > _cfg.privilege) {
					topwindow.window.visible = false;
					break;
				}
				else if (topwindow.privilege <= _cfg.privilege) {
					PopUpManager.removePopUp(topwindow.window);
					_stack.shift();
				}
			}
			sangoFacade.removeMediatorByType(_cfg.mcls);
			var _win:IFlexDisplayObject = PopUpManager.createPopUp(_app, _cfg.cls, _cfg.modal);
			// event handler
			setupEventHandler(_win as IEventDispatcher);
			var _m:AbstractMediator = new _cfg.mcls(_win);
			_stack.unshift(new PopupHolder(_win, _cfg, _m));
			sangoFacade.registerMediatorByType(_m);
			
			return this;
		}
		private function setupEventHandler(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(TitleWindowBoundsEvent.WINDOW_MOVING, onPopupWindowMoving);
			dispatcher.addEventListener(Event.CLOSE, onCloseWindow);
		}
/**
 * 把顶层窗口弹出, 回退到上一个窗口
 */
private function onCloseWindow(event:Event):void {
	(topwindow.window as IEventDispatcher).dispatchEvent(new GameEvent(SangoConst.CANCEL_WINDOW, null));
	this.popWindow(topwindow.privilege);
}
/**
* 控制一下弹出窗口的移动问题, 限制在屏幕里面
* @param	evt
*/
private function onPopupWindowMoving(evt:TitleWindowBoundsEvent):void {
if (evt.afterBounds.left < 0) {
evt.afterBounds.left = 0;
} else if (evt.afterBounds.right > Config.SCREEN_WIDTH) {
evt.afterBounds.left = Config.SCREEN_WIDTH - evt.afterBounds.width;
}
if (evt.afterBounds.top < 0) {
evt.afterBounds.top = 0;
} else if (evt.afterBounds.bottom > Config.SCREEN_HEIGHT) {
evt.afterBounds.top = Config.SCREEN_HEIGHT - evt.afterBounds.height;
}
}
		//
		// pop window
		//
		public function popWindow(privilege:int):void {
			while (_stack.length > 0 && topwindow.privilege <= privilege) {
				// send the cancel event first
				(topwindow.window as IEventDispatcher).dispatchEvent(new GameEvent(SangoEvent.EVENT_CANCEL_POPUP, 1));
				// then...
				PopUpManager.removePopUp(topwindow.window);
				sangoFacade.removeMediatorByType(topwindow.mcls);
				_stack.shift();
			}
			if (_stack.length > 0) {
				topwindow.window.visible = true;
				PopUpManager.bringToFront(topwindow.window);
			}
		}
	}
	
}

